﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PublicTransport.Views;
using PublicTransport.Views.Controls;
using PublicTransport.Services.Enums;

namespace PublicTransport.Views
{
    partial class SearchView : Form, ISearchView
    {
        public event EventHandler SearchInitiated;
        public event EventHandler ShowNearestStations;

        public SearchView()
        {
            InitializeComponent();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            GradientFill.Fill(
            e.Graphics, ClientRectangle,
            Color.Silver, Color.LightBlue,
            GradientFill.FillDirection.TopToBottom);
            e.Graphics.Dispose();
        }

        public String StartStation
        {
            get
            {
                return fromStationAutoComplete.Text;
            }
            set
            {
                fromStationAutoComplete.Text = value;
            }
        }

        public String DestinationStation
        {
            get
            {
                return destinationStationAutoComplete.Text;
            }
            set
            {
                
                destinationStationAutoComplete.Text = value;
            }
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            EventHandler searchClicked = this.SearchInitiated;
            if (searchClicked != null)
            {
                searchClicked(this, EventArgs.Empty);
            }
        }


        public void SetStationHint(String[] hints)
        {
            fromStationAutoComplete.SetItems(hints);
            destinationStationAutoComplete.SetItems(hints);
        }

        Timer loadTimer = new Timer();
        public void StartProgressBar()
        {
            loadingLabel.Visible = true;
            loadTimer.Interval = 500;
            loadTimer.Tick += new EventHandler(loadTimer_Tick);
            loadTimer.Enabled = true;
        }

        public void StopProgressBar()
        {
            loadTimer.Enabled = false;
            this.Invoke((System.Threading.ThreadStart)delegate
            {
                loadingLabel.Visible = false;
            });

        }

        int numberOfDots = 0;
        void loadTimer_Tick(object sender, EventArgs e)
        {
            String loadingText = "Prebieha príprava dát";
            switch (numberOfDots)
            {
                case 1: loadingText += "."; break;
                case 2: loadingText += ".."; break;
                case 3: loadingText += "..."; break;
                default: numberOfDots = 0; break;
            }
            numberOfDots++;
            loadingLabel.Text = loadingText;
        }



        public int Minutes
        {
            get
            {
                int value=0;
                this.Invoke((System.Threading.ThreadStart)delegate
                {
                    value = (int)minutesNumeric.Value;
                });
                return value;
            }
            set
            {
                minutesNumeric.Value = value;
            }
        }

        public int Hours
        {
            get
            {
                int value=0;
                this.Invoke((System.Threading.ThreadStart)delegate
                {
                    value = (int)hourNumeric.Value;
                });
                return value;
            }
            set
            {
                hourNumeric.Value = value;
            }
        }

        Timer messageTimer = new Timer();
        public void DisplayError(String message, bool popout)
        {
            if (popout == false)
            {
                warningLbl.Text = message;
                warningLbl.Show();
                messageTimer.Interval = 1000;
                messageTimer.Tick += new EventHandler(messageTimer_Tick);
                messageTimer.Enabled = true;

            }
            else
            {
                MessageBox.Show(message, "Chyba");
            }

        }

        void messageTimer_Tick(object sender, EventArgs e)
        {
            warningLbl.Hide();
        }




        public DayTypeEnum? DayType
        {
            get
            {
                switch (dayTypeUpDown1.SelectedIndex)
                {
                    case 0: return DayTypeEnum.Work; break;
                    case 1: return DayTypeEnum.Weekend; break;
                    case 2: return DayTypeEnum.Holiday; break;
                    default: return null;
                }
            }
            set
            {
                switch (value.Value)
                {
                    case DayTypeEnum.Work : dayTypeUpDown1.SelectedIndex = 0; break;
                    case DayTypeEnum.Weekend: dayTypeUpDown1.SelectedIndex = 1; break;
                    case DayTypeEnum.Holiday: dayTypeUpDown1.SelectedIndex = 2; break;
                }
            }
        }


        public bool SearchButtonEnabled
        {
            get
            {
                return searchBtn.Enabled; 
            }
            set
            {
                 this.Invoke((System.Threading.ThreadStart)delegate
                {
                    searchBtn.Enabled = value;
                });
            }
        }

        private void nearestStationsMenuItem1_Click(object sender, EventArgs e)
        {
            if (ShowNearestStations != null)
                ShowNearestStations(this, new EventArgs());
        }




        #region ISearchView Members


        public void StartProgressBarSearch()
        {
            label2.Show();
        }

        public void StopProgressBarSearch()
        {
            label2.Hide();
        }

        #endregion
    }
}